var a = require("../../../@babel/runtime/helpers/toConsumableArray"), t = getApp(), e = 2 * t.globalData.statusBarHeight + 20;

Page({
    data: {
        statusBarHeight: e,
        datainfo: [],
        moreArr: [],
        hint: !1,
        allNum: 0,
        isLoading: !1,
        page: 1,
        plugin_name: ""
    },
    onLoad: function(a) {
        a.id && this.setData({
            id: a.id
        }), a.goods_id && this.setData({
            goods_id: a.goods_id
        }), a.tag && this.setData({
            tag: a.tag
        }), this.getData();
    },
    back: function() {
        wx.navigateBack({
            delta: 1
        });
    },
    submit: function() {
        for (var t = this, e = this.data.datainfo, n = this.data.info.data, i = this.data.allNum, s = 0; s < n.length; s++) if (n[s].answer && n[s].answer.fraction) {
            if (2 == n[s].type) for (var r in n[s].answer.fraction) i += Number(n[s].answer.fraction[r]);
            if (1 == n[s].type) {
                var o = [];
                for (var d in n[s].answer.fraction) o[d] = Number(n[s].answer.fraction[d]);
                var c = o.filter(function(a, t, e) {
                    if (a) return a;
                });
                i += Math.max.apply(Math, a(c));
            }
        }
        this.setData({
            allNum: i
        });
        for (var u = 0; u < n.length; u++) if (n[u].id && n[u].required && !e[u]) return this.setData({
            hint: !0
        }), void setTimeout(function() {
            t.setData({
                hint: !1
            });
        }, 1500);
        this.pushData();
    },
    checkboxChange: function(a) {
        var t = this.data.datainfo, e = a.currentTarget.dataset.ind, n = a.currentTarget.dataset.indx, i = this.data.info.data[e], s = i.id, r = i.questionnaire_id, o = [], d = [], c = i.answer.answer[n], u = i.answer.fraction[n];
        if ([].push(i.answer.fraction[n]), [].push(i.answer.answer[n]), 0 == t.length) o.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: s
        }, {
            questionnaire_id: r
        }), o[0].answer[n] = i.answer.answer[n], o[1].fraction[n] = i.answer.fraction[n], 
        d.push(o), t[e] = o; else if (this.cheackarr(t, e, s)) for (var f = 0; f < t[e].length; f++) t[e][f].answer && (t[e][f].answer[n] ? delete t[e][f].answer[n] : t[e][f].answer[n] = c), 
        t[e][f].fraction && (t[e][f].fraction[n] ? delete t[e][f].fraction[n] : t[e][f].fraction[n] = u); else o.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: s
        }, {
            questionnaire_id: r
        }), o[0].answer[n] = i.answer.answer[n], o[1].fraction[n] = i.answer.fraction[n], 
        d.push(o), t[e] = o;
        this.setData({
            datainfo: t
        });
    },
    multiple: function(a) {
        console.log(a);
        var t = this.data.datainfo, e = a.currentTarget.dataset.ind, n = a.currentTarget.dataset.indx, i = this.data.info.data[e], s = i.id, r = i.questionnaire_id, o = [], d = [], c = i.answer.answer[n], u = i.answer.fraction[n];
        if ([].push(i.answer.fraction[n]), [].push(i.answer.answer[n]), 0 == t.length) o.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: s
        }, {
            questionnaire_id: r
        }), o[0].answer[n] = i.answer.answer[n], o[1].fraction[n] = i.answer.fraction[n], 
        d.push(o), t[e] = o; else if (this.cheackarr(t, e, s)) for (var f = 0; f < t[e].length; f++) t[e][f].answer && (t[e][f].answer[n] ? delete t[e][f].answer[n] : t[e][f].answer[n] = c), 
        t[e][f].fraction && (t[e][f].fraction[n] ? delete t[e][f].fraction[n] : t[e][f].fraction[n] = u); else o.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: s
        }, {
            questionnaire_id: r
        }), o[0].answer[n] = i.answer.answer[n], o[1].fraction[n] = i.answer.fraction[n], 
        d.push(o), t[e] = o;
        this.setData({
            datainfo: t
        });
    },
    cheackarr: function(a, t, e) {
        if (!a[t]) return !1;
        for (var n = 0; n < a[t].length; n++) if (a[t][n].id == e) return !0;
    },
    radioChange: function(a) {
        console.log(a);
        var t = a.currentTarget.dataset.ind, e = [], n = this.data.info.data, i = a.currentTarget.dataset.id, s = n[t].questionnaire_id, r = this.data.datainfo, o = a.currentTarget.dataset.indx, d = this.data.info.data[t];
        e.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: i
        }, {
            questionnaire_id: s
        }), e[0].answer[o] = d.answer.answer[o], e[1].fraction[o] = d.answer.fraction[o], 
        r[t] = e, console.log(r), this.setData({
            datainfo: r
        });
    },
    radio: function(a) {
        var t = a.currentTarget.dataset.ind, e = [], n = this.data.info.data, i = a.currentTarget.dataset.id, s = n[t].questionnaire_id, r = this.data.datainfo, o = a.currentTarget.dataset.indx, d = this.data.info.data[t];
        e.push({
            answer: {}
        }, {
            fraction: {}
        }, {
            id: i
        }, {
            questionnaire_id: s
        }), e[0].answer[o] = d.answer.answer[o], e[1].fraction[o] = d.answer.fraction[o], 
        r[t] = e, console.log(r), this.setData({
            datainfo: r
        });
    },
    trimSpace: function(a) {
        for (var t = 0; t < a.length; t++) " " != a[t] && null != a[t] && void 0 !== a[t] || (a.splice(t, 1), 
        t -= 1);
        return a;
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        if (this.data.isLoading) {
            var a = this.data.page + 1;
            this.setData({
                page: a
            }), this.getMore();
        }
    },
    onShareAppMessage: function() {
        var a = wx.getStorageSync("yz_uid"), t = "";
        a && (t = a);
        var e = {
            title: navtitle.desc,
            path: "/packageE/questionnaire/questions/questions?tag=+" + this.data.tag + "&mid=" + t + "&id=" + this.data.id
        };
        return this.data.goods_id && (e.path = e.path + "&goods_id=" + this.data.goods_id), 
        e;
    },
    pushData: function() {
        var a = this, e = t.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
        t._postNetWork({
            url: e,
            data: {
                data: JSON.stringify(this.data.datainfo),
                answerScore: "answerScore",
                questionnaire_id: a.data.id ? a.data.id : ""
            },
            success: function(t) {
                var e = t.data;
                if (console.log(e), 1 == e.result) {
                    var n = e.data.answerScore, i = JSON.stringify(n);
                    console.log(i), wx.navigateTo({
                        url: "/packageE/questionnaire/surveyResult/surveyResult?tag=" + a.data.tag + "&score=" + n.score + "&questionnaire_id=" + n.questionnaire_id + "&rewardPoint=" + n.rewardPoint + "&allNum=" + a.data.allNum + "&goods_id=" + a.data.goods_id + "&plugin_point_name=" + n.plugin_point_name
                    }), wx.setStorageSync("surveyResult", i);
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getData: function() {
        var a = this, e = t.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
        t._getNetWork({
            url: e,
            data: {
                answer: "answer",
                questionnaire_id: a.data.id ? a.data.id : "",
                goods_id: a.data.goods_id ? a.data.goods_id : "",
                page: a.data.page
            },
            success: function(t) {
                var e = t.data;
                console.log(e), 1 == e.result ? (a.setData({
                    info: e.data.answer.choice,
                    id: e.data.answer.questionnaire_id,
                    title: e.data.answer.title,
                    current_page: e.data.answer.choice.current_page,
                    last_page: e.data.answer.choice.last_page,
                    plugin_name: e.data.plugin_name
                }), a.data.current_page >= a.data.last_page ? a.setData({
                    isLoading: !1
                }) : a.setData({
                    isLoading: !0
                })) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500,
                    success: function(a) {
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1600);
                    }
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getMore: function() {
        var a = this, e = t.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
        t._getNetWork({
            url: e,
            data: {
                answer: "answer",
                questionnaire_id: a.data.id ? a.data.id : "",
                goods_id: a.data.goods_id ? a.data.goods_id : "",
                page: a.data.page
            },
            success: function(t) {
                var e = t.data;
                console.log(e), 1 == e.result ? (a.setData({
                    "info.data": a.data.info.data.concat(e.data.answer.choice.data),
                    id: e.data.answer.questionnaire_id,
                    title: e.data.answer.title,
                    current_page: e.data.answer.choice.current_page,
                    last_page: e.data.answer.choice.last_page
                }), a.data.current_page >= a.data.last_page ? a.setData({
                    isLoading: !1
                }) : a.setData({
                    isLoading: !0
                })) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    }
});